<?php
/**
 * Box.net Uploader
 *
 * @version 1.0
 * @author martin maly <author@php-suit.com>
 * @copyright (C) 2008 martin maly
 */

/**
 * Prerequisities:
 * 1. Register as developer at enabled.box.net
 * 2. Register your application and obtain the API key
 * 3. Copy your api key at the appropriate place below
 * 4. Prepare regular box.net account for data storage
 * 
 * Usage:
 * 	$b=new boxnetsuit();
	$b->authorize('box.net user name', 'box.net password');
	$b->upload('./anyfile.zip'); 
 */

class boxnetsuit {
	private $return;
	private $status;
	private $token;
	private $auth;
	
	//YOUR API KEY GOES HERE!
	const apikey='2r96gpqhg6f0ukqbcmqetldzfjaut30s';

	function __construct(){
		$this->auth = false;
	}

	private function _send($action, $getdata=array()){
		$get = http_build_query($getdata);
		$host='ssl://www.box.net';
		$path = '/api/1.0/rest?api_key='.self::apikey.'&action='.$action;
		if ($this->auth) $path .= '&auth_token='.$this->token;
		if ($get) $path.='&'.$get;

		$fp = fsockopen($host, 443);
		// then just
		fputs($fp, "GET $path HTTP/1.1\r\n");
		fputs($fp, "Host: $host\r\n");
		fputs($fp, "Connection: close\r\n");
		fputs($fp, "\r\n");
		$return='';
		while (!feof($fp)) {
			$return .= fgets($fp, 128);
		}
		fclose($fp);
		
		$p=strpos($return,"\r\n\r\n");
	
		$this->return = trim(substr($return,$p));
		if (!$return) return false;
		if (stripos($return, '200 OK')) return true;
		return false;
	}

	/**
	 * Get authentification token from box.net site (i.e. "login")
	 * @param string $user
	 * @param string $password
	 * @return boolean
	 */
	public function authorize($user, $password){
		if ($this->_send('authorization',array('login'=>$user,'password'=>$password,'method'=>'1'))){
			$s=$this->return;
			$status = '';
			if (preg_match('/\<status\>(.*)\<\/status\>/is', $s, $arr)){
				$status = $arr[1];
			}
			$token = '';
			if (preg_match('/\<auth\_token\>(.*)\<\/auth\_token\>/is', $s, $arr)){
				$token = $arr[1];
			}

			$this->status = $status;
			$this->token = $token;
			$this->auth = ($status == 'logged')?true:false;
			return true;
		} else {
			return false;
		}
	}

	/**
	 * just for fun
	 */
	function gettree(){
		if (!$this->auth) return false;
		$this->_send('get_account_tree',array('folder_id'=>0,'params[]'=>'nozip'));
		echo htmlspecialchars(nl2br(print_r($this->return,true)));
	}

	
	/**
	 * Main backup uploader
	 */
	function upload($file, $remote='', $folder_id = 0){

		if (!file_exists($file)) return false;
		if (!$this->auth) return false;
		
		if (!$remote)
			$remote_filename = 'backup' . date("Y-m-d-H-i-s") . '.tgz';
		else 
			$remote_filename = $remote;

		$host = '/api/1.0/upload/'.$this->token.'/'.$folder_id;

		srand((double)microtime()*1000000);
		$boundary='---------------------------'.substr(md5(rand(0,32000)),0,10);

		$header = "POST $host HTTP/1.1\r\n";
		$header .= "Host: upload.box.net\r\n";
		$header .= "Content-type: multipart/form-data, boundary=$boundary\r\n";

		$data = '';
		$data .="--$boundary\r\n";
		$data .= "Content-Disposition: form-data; name=\"share\"\r\n";
		$data .= "\r\n".'1'."\r\n";
		$data .="--$boundary\r\n";

		$data .="--$boundary\r\n";
		$data .= "Content-Disposition: form-data; name=\"file\"; filename=\"$remote_filename\"\r\n";
		$data .= "Content-Type: application/octet-stream;\r\n";
		$data .= "\r\n".(file_get_contents($file))."\r\n";
		$data .="--$boundary--\r\n";
		$header .= "Content-length: " . strlen($data) . "\r\n\r\n";

		$fp = fsockopen('upload.box.net', 80);
		// then just
		fputs($fp, $header.$data);
		while (!feof($fp)) {
			echo fgets($fp, 128);
		}
		fclose($fp);

		echo htmlspecialchars(nl2br(print_r($this->return,true)));
	}


}
?>